package Servlet;

import dao.Impl.ProductImpl;
import dao.ProductDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ClossName:BatchDeleProductServlet
 * Pakage:Servlet
 * Description
 *
 * @Author L
 * @Create 2023/11/7 11:19
 * @Version 2023.1.3
 */
@WebServlet("/batchdele")
public class BatchDeleProductServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");

        //获取数据
        String check = req.getParameter("number");
        //字符串分割
        String[] nub = check.split(",");

        //调用方法
        ProductDao productDao = new ProductImpl();
        int rows = -1;
        for (int i=0 ;i<nub.length;i++ ){
            //字符串转int
            Integer number = Integer.valueOf(nub[i]);
            //调用方法
             rows = productDao.deleteProduct(number);
        }
        if (rows > 0){
            resp.getWriter().write("删除成功");
            return;
        }else {
            resp.getWriter().write("失败");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
